//
//  AutoTestingView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/10.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class AutoTestingView: UIView {

    override init(frame : CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 4 * YXWidthScale
        self.layer.borderWidth = 2 * YXWidthScale
        self.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        self.backgroundColor = UIColor.white
    }
    
    func loadContent(title : String,message : String,infos : Array<Dictionary<String,String>>) -> Void {
        let titleLbl = UILabel.init()
        titleLbl.textColor = UIColor.black
        let font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        titleLbl.font = font
        titleLbl.text = title
        let size = "检测报告".StringSize(font: font)
        titleLbl.frame = CGRect.init(origin: CGPoint.init(x: self.width() / 2 - size.width / 2, y: 58 * YXHeightScale), size: size)
        self.addSubview(titleLbl)
        let baseLine = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 42 * YXWidthScale, y: titleLbl.top() + titleLbl.height() / 2), size: CGSize.init(width: 220 * YXWidthScale, height: YXHeightScale * 2)))
        baseLine.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.addSubview(baseLine)
        
        let baseLine1 = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: self.width() - 262 * YXWidthScale, y: titleLbl.top() + titleLbl.height() / 2), size: CGSize.init(width: 220 * YXWidthScale, height: YXHeightScale * 2)))
        baseLine1.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.addSubview(baseLine1)
        let msgLbl = UILabel.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 636 * YXWidthScale, height: 88 * YXHeightScale)))
        msgLbl.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        msgLbl.textColor = UIColor.colorWithHexString(hex: "646464")
        msgLbl.text = message
        msgLbl.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        msgLbl.layer.borderWidth = 2 * YXWidthScale
        msgLbl.textAlignment = .center
        msgLbl.backgroundColor = UIColor.colorWithHexString(hex: "f8f9fb")
        let back = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: baseLine.left(), y: titleLbl.bottom() + 50 * YXHeightScale), size: CGSize.init(width: 636 * YXWidthScale, height: 0)))
        back.layer.borderWidth = 2 * YXWidthScale
        back.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        back.layer.cornerRadius = 4 * YXWidthScale
        back.addSubview(msgLbl)
        self.addSubview(back)
        if infos.count > 0 {
            var idx = 0
            for item in infos {
//                let view = content(title: item["title"]!, content: item["content"]!, frame: CGRect.init(x: 0, y: 88 * YXHeightScale + 80 * YXHeightScale * CGFloat(idx), width: 636 * YXWidthScale, height: 80 * YXHeightScale))
                let view = content(title: item["title"]!, content: item["content"]!, frame: CGRect.init(x: 0, y: back.height(), width: 636 * YXWidthScale, height: 80 * YXHeightScale))
                if idx % 2 == 0{
                    view.backgroundColor = UIColor.colorWithHexString(hex: "fcfcfc")
                }else{
                    view.backgroundColor = UIColor.colorWithHexString(hex: "f8f9fb")
                }
                back.addSubview(view)
                back.frame = CGRect.init(origin: back.frame.origin, size: CGSize.init(width: back.width(), height: view.bottom()))
                idx = idx + 1
            }
        }
        self.frame = CGRect.init(x: self.left(), y: self.top(), width: self.width(), height: back.bottom() + 64 * YXHeightScale)
    }
    
    func configContent(model : PhoneInfoModel) -> Void {
        
    }
    
    func content(title : String,content : String,frame : CGRect)->UIView{
        let tray = UIView.init()
        tray.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        tray.frame = frame
        let line = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 308 * YXWidthScale, y: 0), size: CGSize.init(width: 2 * YXWidthScale, height: frame.size.height)))
        line.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        tray.addSubview(line)
        let font = UIFont.systemFont(ofSize: 26 * YXWidthScale)
        let size = title.StringSize(font: font)
        let label = UILabel.init(frame: CGRect.init(origin: CGPoint.init(x: 68 * YXWidthScale, y: frame.size.height / 2 - size.height / 2), size: size))
        label.numberOfLines = 3
        label.textColor = UIColor.colorWithHexString(hex: "646464")
        label.font = font
        label.textAlignment = .left
        label.text = title
        tray.addSubview(label)
        
        let size1 = content.stringSize(font: font, maxWid: frame.size.width - 408 * YXWidthScale)
        let cnt = UILabel.init(frame: CGRect.init(origin: CGPoint.init(x: line.right() + 50 * YXWidthScale, y: label.top()), size: size1))
        cnt.font = font
        cnt.numberOfLines = 4
        cnt.textAlignment = .center
        cnt.textColor = UIColor.colorWithHexString(hex: "646464")
        cnt.text = content
        tray.addSubview(cnt)
        let height = frame.size.height - size.height + size1.height
        let separator = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: tray.height() - 2 * YXHeightScale), size: CGSize.init(width: tray.width(), height: 2 * YXHeightScale)))
        separator.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        tray.addSubview(separator)
        if height > frame.size.height {
            tray.frame = CGRect.init(origin: tray.frame.origin, size: CGSize.init(width: tray.frame.size.width, height: height))
            label.frame = CGRect.init(x: 68 * YXWidthScale, y: height / 2 - size.height / 2, width: size.width, height: size.height)
            cnt.frame = CGRect.init(x: cnt.frame.origin.x, y: cnt.frame.origin.y, width: size1.width, height: size1.height)
            separator.frame = CGRect.init(origin: CGPoint.init(x: 0, y: height - 2 * YXHeightScale), size: CGSize.init(width: tray.width(), height: 2 * YXHeightScale))
            line.frame = CGRect.init(origin: CGPoint.init(x: 308 * YXWidthScale, y: 0), size: CGSize.init(width: 2 * YXWidthScale, height: height))
        }
        return tray
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
